#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

pub mod commands;
pub mod ipc;
pub mod kline;


use fudata::db;
use tauri::api::shell;
use tauri::AboutMetadata;
use tauri::CustomMenuItem;
use tauri::Manager;
use tauri::Menu;
use tauri::MenuEntry;
use tauri::MenuItem;
use tauri::Submenu;

use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let ctx = tauri::generate_context!();
    let _dbrs = db::init().await?;

    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![
            commands::greet,
            commands::greet2,
            commands::greet3,
            commands::list_securities,
            commands::get_security,
            commands::list_kline_flats,
            commands::list_kline_datapoints,
            commands::list_trend_line_klines,
            commands::get_kline_flat_table,
            commands::update_kline_flat_table_column,
            commands::list_indicators_correlation_matrix,
            commands::list_indicator_combinations,
            commands::list_trade_strategies,
            commands::get_trade_strategy,
            commands::verify_trade_strategy,
            commands::update_trade_strategy,
            commands::publish_trade_strategy,
            commands::update_trade_strategy_indicator,
            commands::update_trade_strategy_maline,
            commands::list_trade_bots,
            commands::list_trade_orders,
            commands::get_trade_bot,
            commands::update_trade_bot,
            commands::start_trade_bot_backtest,
            commands::list_indicators,
            commands::list_history,
        ])
        .setup(|_app| {
            // let _window = WindowBuilder::new(app, "main", WindowUrl::default())
            //     .title("Fuda Trader")
            //     .inner_size(800.0, 550.0)
            //     .min_inner_size(400.0, 200.0)
            //     .build()
            //     .expect("Unable to create window");

            Ok(())
        })
        .menu(Menu::with_items([
            #[cfg(target_os = "macos")]
            MenuEntry::Submenu(Submenu::new(
                &ctx.package_info().name,
                Menu::with_items([
                    MenuItem::About(ctx.package_info().name.clone(), AboutMetadata::default())
                        .into(),
                    MenuItem::Separator.into(),
                    MenuItem::Services.into(),
                    MenuItem::Separator.into(),
                    MenuItem::Hide.into(),
                    MenuItem::HideOthers.into(),
                    MenuItem::ShowAll.into(),
                    MenuItem::Separator.into(),
                    MenuItem::Quit.into(),
                ]),
            )),
            MenuEntry::Submenu(Submenu::new(
                "File",
                Menu::with_items([MenuItem::CloseWindow.into()]),
            )),
            MenuEntry::Submenu(Submenu::new(
                "Edit",
                Menu::with_items([
                    MenuItem::Undo.into(),
                    MenuItem::Redo.into(),
                    MenuItem::Separator.into(),
                    MenuItem::Cut.into(),
                    MenuItem::Copy.into(),
                    MenuItem::Paste.into(),
                    #[cfg(not(target_os = "macos"))]
                    MenuItem::Separator.into(),
                    MenuItem::SelectAll.into(),
                ]),
            )),
            MenuEntry::Submenu(Submenu::new(
                "View",
                Menu::with_items([MenuItem::EnterFullScreen.into()]),
            )),
            MenuEntry::Submenu(Submenu::new(
                "Window",
                Menu::with_items([MenuItem::Minimize.into(), MenuItem::Zoom.into()]),
            )),
            // You should always have a Help menu on macOS because it will automatically
            // show a menu search field
            MenuEntry::Submenu(Submenu::new(
                "Help",
                Menu::with_items([CustomMenuItem::new("Learn More", "Learn More").into()]),
            )),
        ]))
        .on_menu_event(|event| {
            let event_name = event.menu_item_id();
            match event_name {
                "Learn More" => {
                    let url = "https://github.com/probablykasper/fuquant-trader".to_string();
                    shell::open(&event.window().shell_scope(), url, None).unwrap();
                }
                _ => {}
            }
        })
        .run(ctx)
        .expect("error while running tauri application");

    Ok(())
}
